﻿using RecruitStudents.Utility;
using RecruitStudents.Utility.Json;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecruitStudents.Client.BLL.Impl
{
    public class BLLScore
    {
        DAL.DALScore dal = new DAL.DALScore();
        public DataTable GetScoreDisplayTable5_2(string strExamRoomNo)
        {
            string strSql = @"SELECT 
	                        IdentityCard,
	                        LotNO,
	                        NMT_SignUpID,
	                        Score1,
	                        Score2,
	                        Score3,
	                        Score4,
	                        Score5,
	                        dbo.getMaxMin5(Score1,Score2,Score3,Score4,Score5) as ScoreAll1,
	                        Score6,
	                        Score7,
	                        Score8,
	                        Score9,
	                        Score10,
	                        dbo.getMaxMin5(Score6,Score7,Score8,Score9,Score10) as ScoreAll2
                        FROM 
                        (
	                        select 
	                            IdentityCard,
		                        LotNO,
		                        NMT_SignUpID,
		                        dbo.getSort5(1,Score1,Score2,Score3,Score4,Score5) as Score1,
		                        dbo.getSort5(2,Score1,Score2,Score3,Score4,Score5) as Score2,
		                        dbo.getSort5(3,Score1,Score2,Score3,Score4,Score5) as Score3,
		                        dbo.getSort5(4,Score1,Score2,Score3,Score4,Score5) as Score4,
		                        dbo.getSort5(5,Score1,Score2,Score3,Score4,Score5) as Score5,
		                        dbo.getSort5(1,Score6,Score7,Score8,Score9,Score10) as Score6,
		                        dbo.getSort5(2,Score6,Score7,Score8,Score9,Score10) as Score7,
		                        dbo.getSort5(3,Score6,Score7,Score8,Score9,Score10) as Score8,
		                        dbo.getSort5(4,Score6,Score7,Score8,Score9,Score10) as Score9,
		                        dbo.getSort5(5,Score6,Score7,Score8,Score9,Score10) as Score10
	                        from
	                        (
		                        select 
			                        A.LotNO,
			                        B.NMT_SignUpID,
			                        ISNULL((select Score1 from TB_Pad_TeacherScoreList T where T.LotNO = A.LotNO AND T.AGT_ID = A.AGT_ID AND T.LoginName = B.Teacher1),0) as Score1,
			                        ISNULL((select Score1 from TB_Pad_TeacherScoreList T where T.LotNO = A.LotNO AND T.AGT_ID = A.AGT_ID AND T.LoginName = B.Teacher2),0) as Score2,
			                        ISNULL((select Score1 from TB_Pad_TeacherScoreList T where T.LotNO = A.LotNO AND T.AGT_ID = A.AGT_ID AND T.LoginName = B.Teacher3),0) as Score3,
			                        ISNULL((select Score1 from TB_Pad_TeacherScoreList T where T.LotNO = A.LotNO AND T.AGT_ID = A.AGT_ID AND T.LoginName = B.Teacher4),0) as Score4,
			                        ISNULL((select Score1 from TB_Pad_TeacherScoreList T where T.LotNO = A.LotNO AND T.AGT_ID = A.AGT_ID AND T.LoginName = B.Teacher5),0) as Score5,
			                        ISNULL((select Score2 from TB_Pad_TeacherScoreList T where T.LotNO = A.LotNO AND T.AGT_ID = A.AGT_ID AND T.LoginName = B.Teacher1),0) as Score6,
			                        ISNULL((select Score2 from TB_Pad_TeacherScoreList T where T.LotNO = A.LotNO AND T.AGT_ID = A.AGT_ID AND T.LoginName = B.Teacher2),0) as Score7,
			                        ISNULL((select Score2 from TB_Pad_TeacherScoreList T where T.LotNO = A.LotNO AND T.AGT_ID = A.AGT_ID AND T.LoginName = B.Teacher3),0) as Score8,
			                        ISNULL((select Score2 from TB_Pad_TeacherScoreList T where T.LotNO = A.LotNO AND T.AGT_ID = A.AGT_ID AND T.LoginName = B.Teacher4),0) as Score9,
			                        ISNULL((select Score2 from TB_Pad_TeacherScoreList T where T.LotNO = A.LotNO AND T.AGT_ID = A.AGT_ID AND T.LoginName = B.Teacher5),0) as Score10,
			                        B.IdentityCard
		                        from TB_Pad_ScoreDisplay		  A 
		                        left join TB_Pad_StudentScoreList B ON A.SignedID = B.SignedID
		                        where A.AGT_ID in 
		                        (
			                        select top 1 AGT_ID from TB_Pad_ScoreDisplay where ExamRoomNO = '{0}' and Flag = '0' order by CreateTime
		                        )   
	                        ) TT
                        ) TTT
                        order by LotNO 
            ";
            strSql = string.Format(strSql, strExamRoomNo);
            return dal.GetTable(strSql);
        }


        public DataTable GetScoreDisplayTable5(string strExamRoomNo)
        {
            string strSql = @"select 
                                A.LotNO,
                                B.NMT_SignUpID,
                                dbo.getSort5(1,Score1,Score2,Score3,Score4,Score5) as Score1,
                                dbo.getSort5(2,Score1,Score2,Score3,Score4,Score5) as Score2,
                                dbo.getSort5(3,Score1,Score2,Score3,Score4,Score5) as Score3,
                                dbo.getSort5(4,Score1,Score2,Score3,Score4,Score5) as Score4,
                                dbo.getSort5(5,Score1,Score2,Score3,Score4,Score5) as Score5,
                                ISNULL(TatolScore,0) as TatolScore,
                                B.IdentityCard
                            from TB_Pad_ScoreDisplay	   A 
                            left join TB_Pad_StudentScoreList B ON A.LotNO = B.LotNO AND  A.SignedID = B.SignedID
                            where A.AGT_ID in 
                                (
                                select top 1 AGT_ID from TB_Pad_ScoreDisplay where ExamRoomNO = '{0}' and Flag = '0' order by CreateTime
                                )   
                            order by A.LotNO";
            strSql = string.Format(strSql, strExamRoomNo);
            return dal.GetTable(strSql);
        }


        public DataTable GetScoreDisplayTable7(string strExamRoomNo)
        {
            string strSql = @"select 
                                A.LotNO,
                                B.NMT_SignUpID,
                                dbo.getSort7(1,Score1,Score2,Score3,Score4,Score5,Score6,Score7) as Score1,
                                dbo.getSort7(2,Score1,Score2,Score3,Score4,Score5,Score6,Score7) as Score2,
                                dbo.getSort7(3,Score1,Score2,Score3,Score4,Score5,Score6,Score7) as Score3,
                                dbo.getSort7(4,Score1,Score2,Score3,Score4,Score5,Score6,Score7) as Score4,
                                dbo.getSort7(5,Score1,Score2,Score3,Score4,Score5,Score6,Score7) as Score5,
                                dbo.getSort7(6,Score1,Score2,Score3,Score4,Score5,Score6,Score7) as Score6,
                                dbo.getSort7(7,Score1,Score2,Score3,Score4,Score5,Score6,Score7) as Score7,
                                ISNULL(TatolScore,0) as TatolScore,
                                B.IdentityCard
                            from TB_Pad_ScoreDisplay	   A 
                            left join TB_Pad_StudentScoreList B  ON A.LotNO = B.LotNO AND A.SignedID = B.SignedID 
                            where A.AGT_ID in 
                                (
                                select top 1 AGT_ID from TB_Pad_ScoreDisplay where ExamRoomNO = '{0}' and Flag = '0' order by CreateTime
                                )   
                            order by A.LotNO";
            strSql = string.Format(strSql, strExamRoomNo);
            return dal.GetTable(strSql);
        }


        public DataTable GetScoreQueryUp(string strSFYSB, string strExamRoomID, string strTeacherID, string strExaminationID,
            string strExamPlaceID, string strSpecialityKindsNO, string strExamLevel)
        {
            return dal.GetScoreQueryUp(strExamRoomID, strTeacherID, strExaminationID, strSFYSB, strExamPlaceID, strSpecialityKindsNO, strExamLevel);
        }
        public DataTable GetScoreQuery(string strSFYSB, string strExamRoomID, string strTeacherID, string strExaminationID,
            string strExamPlaceID, string strSpecialityKindsNO, string strExamLevel)
        {
            if (strSFYSB == "1")
            {
                DataTable dtTeacher = ServiceHelper.CreateCommService().GetTeacherList(strExamRoomID);
                return dal.GetScoreQuery(strExamRoomID, strTeacherID, strExaminationID, strSFYSB, dtTeacher, strExamPlaceID, strSpecialityKindsNO, strExamLevel);
            }
            else
            {
                return dal.GetScoreQuery(strExamRoomID, strTeacherID, strExaminationID, strSFYSB, null, strExamPlaceID, strSpecialityKindsNO, strExamLevel);
            }
        }

        public DataTable GetTeacherScoreList(string strAllowExamID, string strExaminationID, string strSpecialityKindsNO, string strExamLevel)
        {
            return dal.GetTeacherScoreList(strAllowExamID, strExaminationID, strSpecialityKindsNO, strExamLevel);
        }

        public JsonMessage SaveUpData(string strAllowExamID, string strSpecialityKindsNO, string strExamLevel, string strExamPlaceID, string strIsPass)
        {
            JsonMessage message = new JsonMessage();
            List<string> listAllowExam = new List<string>();
            List<string> listIsPass = new List<string>();
            listAllowExam = strAllowExamID.Split(',').ToList<string>();
            listIsPass = strIsPass.Split(',').ToList<string>();
            if (listAllowExam.Count != listIsPass.Count)
            {
                message.State = "false";
                message.Msg = "您选择数据有误,准考证号与是否通过数不匹配";
            }
            else
            {
                string strMessage = dal.SaveUpData(listAllowExam, listIsPass, strExamLevel, strExamPlaceID, strSpecialityKindsNO);
                if (strMessage == "")
                {
                    List<TB_Pad_StudentScoreList> list = dal.GetPadStudentScoreList(strSpecialityKindsNO, strExamLevel, strExamPlaceID, "1");
                    if (list != null && list.Count > 0)
                    {
                        List<TB_Pad_StudentScore_Up> listUp = new List<TB_Pad_StudentScore_Up>();
                        foreach (TB_Pad_StudentScoreList model in list)
                        {
                            #region  实体赋值
                            TB_Pad_StudentScore_Up modelUp = new TB_Pad_StudentScore_Up();
                            modelUp.StudentScoreListID = model.StudentScoreListID;
                            modelUp.IsPublish = "0";
                            modelUp.AGT_ID = model.AGT_ID;
                            modelUp.AllowExamID = model.AllowExamID;
                            modelUp.CreateTime = model.CreateTime;
                            modelUp.ExamLevel = model.ExamLevel.Value;
                            modelUp.ExamPlaceNO = model.ExamPlaceNO;
                            modelUp.ExamProvince = model.ExamProvince;
                            modelUp.ExamRoomNO = model.ExamRoomNO;
                            modelUp.ExamYear = model.ExamYear;
                            modelUp.GroupNo = model.GroupNo;
                            modelUp.IdentityCard = model.IdentityCard;
                            modelUp.IsPass = model.IsPass;
                            modelUp.IsPrint = model.IsPrint;
                            modelUp.LotNO = model.LotNO;
                            modelUp.Name = model.Name;
                            modelUp.NMT_SignUpID = model.NMT_SignUpID;
                            modelUp.Remark = model.Remark;
                            modelUp.Score1 = model.Score1;
                            modelUp.Score2 = model.Score2;
                            modelUp.Score3 = model.Score3;
                            modelUp.Score4 = model.Score4;
                            modelUp.Score5 = model.Score5;
                            modelUp.Score6 = model.Score6;
                            modelUp.Score7 = model.Score7;
                            modelUp.SignedID = model.SignedID;
                            modelUp.SpecialityKindsNO = model.SpecialityKindsNO;
                            modelUp.TatolScore = model.TatolScore;
                            modelUp.Teacher1 = model.Teacher1;
                            modelUp.Teacher1Name = model.Teacher1Name;
                            modelUp.Teacher2 = model.Teacher2;
                            modelUp.Teacher2Name = model.Teacher2Name;
                            modelUp.Teacher3 = model.Teacher3;
                            modelUp.Teacher3Name = model.Teacher3Name;
                            modelUp.Teacher4 = model.Teacher4;
                            modelUp.Teacher4Name = model.Teacher4Name;
                            modelUp.Teacher5 = model.Teacher5;
                            modelUp.Teacher5Name = model.Teacher5Name;
                            modelUp.Teacher6 = model.Teacher6;
                            modelUp.Teacher6Name = model.Teacher6Name;
                            modelUp.Teacher7 = model.Teacher7;
                            modelUp.Teacher7Name = model.Teacher7Name;
                            listUp.Add(modelUp);
                            #endregion
                        }
                        dal.SavePadStudentScoreUp(listUp, strSpecialityKindsNO, strExamLevel, strExamPlaceID);
                    }
                    message.State = "true";
                    message.Msg = string.Empty;
                }
                else
                {
                    message.State = "false";
                    message.Msg = strMessage;
                }
            }
            return message;
        }

        public JsonMessage SavePublishData(string strAllowExamID, string strSpecialityKindsNO, string strExamLevel, string strExamPlaceID, string strIsPass, string strRemark, string strIsInOut)
        {
            JsonMessage message = new JsonMessage();
            List<string> listAllowExam = new List<string>();
            List<string> listIsPass = new List<string>();
            listAllowExam = strAllowExamID.Split(',').ToList<string>();
            listIsPass = strIsPass.Split(',').ToList<string>();
            if (listAllowExam.Count != listIsPass.Count)
            {
                message.State = "false";
                message.Msg = "您选择数据有误,准考证号与是否通过数不匹配";
            }
            else
            {
                string strMessage = dal.SavePublishData(listAllowExam, listIsPass, strExamLevel, strExamPlaceID, strSpecialityKindsNO, strIsInOut);
                if (strMessage == "")
                {
                    List<TB_Pad_StudentScore_Up> list = null;
                    list = dal.GetPadStudentScorePublishList(strSpecialityKindsNO, strExamLevel, strExamPlaceID);
                    if (list != null && list.Count > 0)
                    {
                        List<TB_Exam_ResultsPublishList> listPublish = new List<TB_Exam_ResultsPublishList>();
                        TB_Exam_ResultsPublish modelPublish = dal.GetExamResultsPublishModel(strExamPlaceID, strSpecialityKindsNO, strExamLevel);
                        if (modelPublish == null)
                        {
                            modelPublish = new TB_Exam_ResultsPublish();
                            modelPublish.CreatTime = DateTime.Now;
                            modelPublish.ExamLevel = strExamLevel.ToSafeInt();
                            modelPublish.ExamPlaceNO = strExamPlaceID;
                            modelPublish.SpecialityKindsNO = strSpecialityKindsNO;
                        }
                        else
                        {
                            modelPublish.Attach();
                        }
                        if (strIsInOut == "1")
                        {
                            modelPublish.IsIn = "1";
                        }
                        if (strIsInOut == "2")
                        {
                            modelPublish.IsOut = "1";
                        }
                        modelPublish.Remartk1 = strRemark;

                        foreach (TB_Pad_StudentScore_Up model in list)
                        {
                            TB_Exam_ResultsPublishList modelPublishList = new TB_Exam_ResultsPublishList();
                            modelPublishList.AllowExamID = model.AllowExamID;
                            modelPublishList.CreatTime = DateTime.Now;
                            modelPublishList.ExamLevel = strExamLevel.ToSafeInt();
                            modelPublishList.ExamPlaceNO = strExamPlaceID;
                            if (strIsInOut == "1")
                            {
                                modelPublishList.IsIn = "1";
                            }
                            else if (strIsInOut == "2")
                            {
                                modelPublishList.IsIn = "1";
                                modelPublishList.IsOut = "1";
                            }
                            modelPublishList.SpecialityKindsNO = strSpecialityKindsNO;
                            TB_Exam_SignUpStudentsSpeciality modelSignUpSpec = null;
                            if (strIsInOut == "2")
                            {
                                int NewExamLevel = Convert.ToInt32(model.ExamLevel) + 1;
                                modelSignUpSpec = dal.GetExamSignUpStudentsSpecialityModel(strExamPlaceID, strSpecialityKindsNO, NewExamLevel.ToString(), model.AllowExamID);
                            }
                            else
                            {
                                modelSignUpSpec = dal.GetExamSignUpStudentsSpecialityModel(strExamPlaceID, strSpecialityKindsNO, strExamLevel, model.AllowExamID);
                            }
                            if (modelSignUpSpec != null)
                            {
                                modelPublishList.SignUpStudentsSpecialityID = modelSignUpSpec.SignUpStudentsSpecialityID;
                            }
                            listPublish.Add(modelPublishList);
                        }
                        dal.SaveResultsPublishData(modelPublish, listPublish, strIsInOut);
                    }
                    message.State = "true";
                    message.Msg = string.Empty;
                }
                else
                {
                    message.State = "false";
                    message.Msg = strMessage;
                }
            }
            return message;
        }

        public DataTable GetExamResultsPublishTable(string strExamPlaceNO, string strSpecialityKindsNO, string strExamLevel)
        {
            return dal.GetTable("select * from TB_Exam_ResultsPublish where ExamPlaceNO='" + strExamPlaceNO + "' and SpecialityKindsNO='" + strSpecialityKindsNO + "' and ExamLevel='" + strExamLevel + "'");
        }
    }
}
